import api from '@/views/se/maintenanceManagement/maintenanceSettlement/api'
import capi from '../maintenanceCollection/api'

const u = [
  'workOrderDetails', 'maintenanceTimeList',
  'repairPartList',
  'repairOtherList'
]

const successObj = {
  workOrderDetails(res) {
    const data = res.rows[1]
    for (const attr in this.formField_custCarInfo) {
      this.formField_custCarInfo[attr] = data[attr] ? data[attr] : ''
    }
  },
  maintenanceTimeList(res) {
    this.$refs['table_3_1'].list = res['rows']
  },
  repairPartList(res) {
    this.$refs['table_3_2'].list = res['rows']
  },
  repairOtherList(res) {
    this.$refs['table_3_3'].list = res['rows']
  }
}

export default {
  loadTable() {
    const { repairOrderCode, repairOrderId, custName } = this.row
    this.$requestAPI({
      apiUrl: api.querySettleAccounts.APIUrl,
      variables: { repairOrderCode, repairOrderId, custName }
    }).then(res => {
      if (res.result === '1') {
        const { other, settlement } = res.rows[0]
        // 为空 清空表格
        if (other[0] === null) {
          this.$refs['q1-3'].list = []
        }
        // 其它费用赋值
        if (other[0] !== null) {
          for (const attr in this.formField_two) {
            this.formField_two[attr] = other[0][attr]
          }
        }
        // 结算信息赋值
        const lis = this.$utils.deepCopy(this.defaultList)
        this.$refs['q1-3'].list = settlement.concat(lis)
      } else {
        this.$message(res.msg ? res.msg : '')
      }
    }).catch(err => {
      if (err.response.data.msg) {
        this.$message(err.response.data.msg)
      } else {
        this.$message(err.message)
      }
    })
  },
  SettlementApplication() {
    const loading = this.$loading({ text: `正在提交申请,请稍等...` })
    this.$requestAPI({
      apiUrl: api.settleAccountsUsingPOST.APIUrl,
      variables: {
        ...this.formField_one,
        list: this.$refs['q1-3'].list
      }
    }).then(res => {
      if (res.result === '1') {
        this.$message('提交结算申请成功!')
        this.$store.commit('tagsView/DEL_VISITED_VIEW', {
          name: 'wExtensiceSubmit',
          path: '/aftermarketMange/maintenanceManagement/maintenanceSettledit'
        })
        this.$router.push({
          name: 'maintenanceSettlement',
          path: '/aftermarketMange/maintenanceManagement/maintenanceSettlement',
          params: { isOver: true }
        })
      } else {
        this.$message(res.msg ? res.msg : '')
      }
    }).catch(err => {
      if (err.response.data.msg) {
        this.$message(err.response.data.msg)
      } else {
        this.$message(err.message)
      }
    }).finally(() => {
      loading.close()
    })
  },
  receivePayment() {
    const loading = this.$loading({ text: `请稍等...` })
    const { repairOrderCode, repairOrderId, updateControlId } = this.row
    this.$requestAPI({
      apiUrl: capi.gatheringUsingPOST.APIUrl,
      variables: {
        ...this.formField_one,
        repairOrderCode, repairOrderId, updateControlId,
        list: this.$refs['q1-3'].$refs['multipleTable'].list
      }
    }).then(res => {
      if (res.result === '1') {
        this.$message('已成功!')
        this.$store.commit('tagsView/DEL_VISITED_VIEW', {
          name: 'wExtensiceSubmit',
          path: '/aftermarketMange/maintenanceManagement/maincollectiondetil'
        })
        this.$router.push({
          name: 'maintenanceCollection',
          params: { isOver: true }
        })
      } else {
        this.$message(res.msg ? res.msg : '')
      }
    }).catch(err => {
      if (err.response.data.msg) {
        this.$message(err.response.data.msg)
      } else {
        this.$message(err.message)
      }
    }).finally(() => {
      loading.close()
    })
  },
  getTabData() {
    const { repairOrderCode, repairOrderId } = this.row
    u.forEach(uName => {
      this.$requestAPI({
        apiUrl: api[uName].APIUrl,
        variables: {
          repairOrderId, repairOrderCode
        }
      }).then(res => {
        if (res.result === '1') {
          successObj[uName].call(this, res)
        } else {
          this.$message(res.msg ? res.msg : '')
        }
      }).catch(err => {
        if (err.response.data.msg) {
          this.$message(err.response.data.msg)
        } else {
          this.$message(err.message)
        }
      })
    })
  }
}
